
package com.javaleague.mensajes;

import com.javaleague.persistencia.DAOUpdateMessage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.javaleague.sesionUsuario.Usuario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author TaixMiguel
 */
public class updateMessage extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
                
        HttpSession session = request.getSession(true);
        Usuario user = (Usuario)session.getAttribute("usuario");
        String idMesage = request.getParameter("id");
        
        if ( user != null && idMesage != null ) {
            int idMessage = Integer.parseInt(idMesage);
//            Tiene derecho a entrar y compruebo a que viene
            if ( request.getParameter("leido") != null) {
                DAOUpdateMessage.marcarLeidoEmail(user.getId(), user.getTokenUser(), idMessage);
            } else if ( request.getParameter("delete") != null) {
                DAOUpdateMessage.marcarBorradoEmail(user.getId(), user.getTokenUser(), idMessage);
            } else {
//                Viene por diversion
                response.sendError(403);
            }
        } else {
//            No tienes derecho a entrar aqui
            response.sendError(403);
        }
        
        
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet updateMessage</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet updateMessage at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}